//
//  NSXCollectViewController.m
//  B段项目
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "NSXCollectViewController.h"
#import "NSXCollectModel.h"
#import "FoodProjectDetailCell.h"
#import "FitHeader.h"
#import "CoreDataManager.h"
#import "FoodDetailModel.h"
#import <UIImageView+WebCache.h>
#import "NSXFirstPageWEBViewController.h"
#import "NSXFirstPageListModel.h"

@interface NSXCollectViewController ()<UITableViewDataSource , UITableViewDelegate>
@property(nonatomic , retain)UITableView *tableView;
@property(nonatomic , retain)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)CoreDataManager *manager;
@property(nonatomic,strong)NSXFirstPageListModel *model;
@end

@implementation NSXCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}

#pragma mark  初始化tableView
-(void)loadView
{
    [super loadView];
    _manager = [CoreDataManager shareManager];
    [self selectAction];
    _model = [[NSXFirstPageListModel alloc]init];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UITableView alloc]init];
    [self.view addSubview:_tableView];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}
#pragma mark cell样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self selectAction].count == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.textLabel.textAlignment = 1;
        cell.textLabel.text = @"你还没有收藏!";
        cell.userInteractionEnabled = NO;
        return cell;
    }
    static NSString *identifier = @"cell";
    FoodProjectDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[FoodProjectDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    _model.ID = [_dataSourceArray[indexPath.row] iD];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.thumbImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSourceArray[indexPath.row] thumb]]];
    cell.titleLabel.text = [_dataSourceArray[indexPath.row] title];
    cell.categoryLabel.text = [_dataSourceArray[indexPath.row]category];
    cell.ageLabel.text = [_dataSourceArray[indexPath.row]age];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 95 * FitHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self selectAction].count == 0) {
        return 1;
    }else {
    return [[self selectAction] count];
    }
}

#pragma mark cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    NSXFirstPageWEBViewController *vc = [[NSXFirstPageWEBViewController alloc]init];
    vc.NSXModel = _model;
    vc.idString = [_dataSourceArray[indexPath.row] iD];
    vc.modelString = [_dataSourceArray[indexPath.row] title];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark CoreData删除方法
-(void)deleteAction
{
    FoodDetailModel *model = [[self selectAction] lastObject];
    [_manager.managedObjectContext deleteObject:model];
    [_manager saveContext];
}
#pragma mark CoreData查询方法
-(NSArray *)selectAction
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"FoodDetailModel"];
    NSArray *array = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    _dataSourceArray = [NSMutableArray arrayWithArray:array];
    return _dataSourceArray;
}
#pragma mark 编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark 允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self selectAction].count == 0) {
        return NO;
    }else {
    return YES;
    }
}
#pragma mark 删除方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        FoodDetailModel *model = [[self selectAction] objectAtIndex:indexPath.row];
        [_manager.managedObjectContext deleteObject:model];
        [_manager saveContext];
        [_tableView reloadData];
    }
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

-(void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
